package com.quyunshuo.module.home.activity.main

import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.quyunshuo.edwinpoet.common.ui.BaseActivity
import com.quyunshuo.edwinpoet.network.bean.ArticleListRes2
import com.quyunshuo.module.home.databinding.HomeActivityMainBinding
import com.quyunshuo.module.home.util.AppExit
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


/**
 * 首页
 *
 * @author Qu Yunshuo
 * @since 5/22/21 2:26 PM
 */
@AndroidEntryPoint
class MainActivity : BaseActivity<HomeActivityMainBinding, HomeViewModel>() {
    companion object {
        fun newInstance(context: Context) {
            context.startActivity(Intent(context, MainActivity::class.java))
        }
    }
    private lateinit var musicAdapter: MusicAdapter


    /**
     * 通过 viewModels() + Hilt 获取 ViewModel 实例
     */
    override val mViewModel by viewModels<HomeViewModel>()


    override fun createVB() = HomeActivityMainBinding.inflate(layoutInflater)


    override fun HomeActivityMainBinding.initView() {
        musicAdapter = MusicAdapter(this@MainActivity, listOf())
        recyclerView.apply {
            layoutManager = LinearLayoutManager(this@MainActivity)
            adapter = musicAdapter
        }

    }


    override fun HomeActivityMainBinding.initListener() {

    }

    override fun initObserve() {
        lifecycleScope.launch {
            delay(2000)
            mViewModel.getAll().collect{

                musicAdapter.updateList(it)
            }
        }
        lifecycleScope.launch {
            mViewModel.poetsMap.collect{
                Log.d("wenlong", "initObserve: $it")
            }
            //val idAndPoetList = mViewModel.getIdAndPoetList()

        }


    }

    private fun processData(data: ArticleListRes2) {

    }

    override fun initRequestData() {
    }

    override fun onBackPressed() {
        AppExit.onBackPressed(this)
    }

    override fun onDestroy() {
        super.onDestroy()
    }


}

